package com.web.dwm;

import com.entity.CatPost;
import com.entity.PageManager;
import com.service.dwm.CatPostService;
import com.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@WebServlet("/catPost")
@MultipartConfig
public class CatPostServlet extends HttpServlet {
    //用户的逻辑层
    private CatPostService catPostService = new CatPostService();

    //request对象
    private HttpServletRequest request;
    //response对象
    private HttpServletResponse response;

    //不管是post还是get请求，都会进入到service
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        synchronized (this) {
            //初始化请求和响应
            this.request = request;
            this.response = response;
            String method = request.getParameter("method");
            switch (method) {
                case "showPic": showPic(); break;
                case "index": queryCatPost(); break;
                case "indexF": queryCatPostF(); break;
                case "del": delUser(); break;
                case "update": update(); break;
            }
        }

    }

    private void queryCatPostF() throws ServletException, IOException {
        PageManager pm = WebUtils.getBean(PageManager.class, request);
        List<CatPost> catPosts = catPostService.queryCatPostsF(pm);
        int totalCount = catPostService.queryCatPostTotalCountF();
        System.out.println(totalCount);
        pm.setTotalCount(totalCount);//总数据数
        pm.setPages();//设置总页数

        request.setAttribute("catPosts", catPosts);
        request.setAttribute("state", "F");
        request.getSession().setAttribute("pm", pm);
        request.setAttribute("outerLi", "catPost");
        request.setAttribute("innerLi", "showCatPost");
        WebUtils.toRequestPageCatPost(request, response, "showCatPost");
    }

    private void update() throws IOException, ServletException {
        int id = Integer.parseInt(request.getParameter("id"));
        //修改
        boolean result = catPostService.updateCatPost(id);
        System.out.println(result);
        PageManager pm = (PageManager) request.getSession().getAttribute("pm");
        request.getSession().setAttribute("msg", result?"修改成功":"修改失败");
        request.getSession().setAttribute("result", result);
        response.sendRedirect("catPost?method=indexF&pn="+(pm==null?"":pm.getPn()));
    }

    private void delUser() throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        PageManager pm = (PageManager) request.getSession().getAttribute("pm");
        if (pm != null) {
            pm.setTotalCount(pm.getTotalCount()-1);
            pm.setPages();
            //删除之后，正好最后一页没了，直接使用最后一页当作当前页
            pm.setPn(pm.getPn()>=pm.getPages()?pm.getPages():pm.getPn());
        }
        boolean b=true;
        if(catPostService.queryCatPost(id).getState().equals("F")){
            b=false;
        }
        boolean result = catPostService.delCatPost(id);
        request.getSession().setAttribute("msg", result?"删除成功":"删除失败");
        request.getSession().setAttribute("result", result);
        if (b){
            response.sendRedirect("catPost?method=index&pn="+((pm==null)?"":pm.getPn()));
        }else{
            response.sendRedirect("catPost?method=indexF&pn="+((pm==null)?"":pm.getPn()));
        }

    }

    private void queryCatPost() throws ServletException, IOException {
        PageManager pm = WebUtils.getBean(PageManager.class, request);
        List<CatPost> catPosts = catPostService.queryCatPosts(pm);
        int totalCount = catPostService.queryCatPostTotalCountT();
        System.out.println(totalCount);
        pm.setTotalCount(totalCount);//总数据数
        pm.setPages();//设置总页数

        request.setAttribute("catPosts", catPosts);
        request.getSession().setAttribute("pm", pm);
        request.setAttribute("state", "T");
        request.setAttribute("outerLi", "catPost");
        request.setAttribute("innerLi", "showCatPost");
        WebUtils.toRequestPageCatPost(request, response, "showCatPost");
    }

    private void showPic() throws IOException {
        String pic = request.getParameter("pic");
        File file = new File(pic);
        //将图片的字节写入到页面
        //jsp中的out写入东西
        OutputStream out = response.getOutputStream();
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
        }
        out.flush();
        out.close();
        in.close();
        response.flushBuffer();//刷新缓存
    }


}
